package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 检查项管理
 */

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    private static Logger logger = Logger.getLogger(CheckItemController.class);

    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("test")
    public String getData(@RequestBody Map map, String id){
        System.out.println(map);
        System.out.println(id);
        return "123";
    }

    //新增检查项
    //1.url上拼接参数/add?name=111
    //(String name)
    //@RequestParam
    //2.post请求,ajax({data})
    //@RequestBody Map data
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        String name = checkItem.getName();
        String code = checkItem.getCode();
        if(StringUtils.isEmpty(name) || StringUtils.isEmpty(code)){
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL_DUPLICATE_NAME);
        }
        //先去查询
        //用checkItem.getName()去查询，是否存在数据
        //调用dubbo接口由service_provier去检测数据是否存在
//        return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL_DUPLICATE_NAME);
        //用checkItem.getCode()去查询，是否存在数据
        try{
            checkItemService.add(checkItem);
        }catch (Exception e){
            //e.printStackTrace();
            logger.error(e);
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //检查项分页查询
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = null;
        try{
            pageResult = checkItemService.pageQuery(queryPageBean);
        }catch (Exception e){
            //e.printStackTrace();
            logger.error(e);
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS,pageResult);
    }

    //删除检查项 /delete?id=100
    @RequestMapping(value="/delete",method = RequestMethod.GET)
    public Result delete(Integer id){
        try{
           int result =  checkItemService.deleteById(id);
           if(CheckItemService.DELETE_BYID_ERROR_CONTAINS == result){
               return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL_CONTAIN);
           }

        }
        catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //编辑检查项
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try{
            checkItemService.edit(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            CheckItem checkItem = checkItemService.findById(id);
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
